package com.hys.ultrasound.utils;

import android.content.Context;

import com.blankj.utilcode.util.LogUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @desc:文件下载工具类
 * @date:2018/6/19 9:38
 * @auther:lp
 * @version:1.0
 */

public class DownloadFileUtil {
    public interface DownloadListener {
        void downloadProgress(long total, long progress);

        void downloadFail();
    }

    public static boolean isDownloading = false;

    /**
     * 下载文件
     *
     * @param url
     * @param downloadListener
     * @param savePath
     */
    public static void downloadFile(final String url, final DownloadListener downloadListener, final String savePath) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    connection = (HttpURLConnection) new URL(url).openConnection();
                    //connection.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");
                    connection.setReadTimeout(120 * 1000);
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        isDownloading = true;
                        long total = connection.getContentLength();
                        InputStream inputStream = connection.getInputStream();
                        File file = new File(savePath);
                        if (file.exists()) file.delete();
                        file.createNewFile();
                        FileOutputStream fileOutputStream = new FileOutputStream(file);

                        byte[] buffer = new byte[1024];
                        int len = 0;
                        long progress = 0;
                        while ((len = inputStream.read(buffer)) != -1) {
                            if (!isDownloading) break;
                            fileOutputStream.write(buffer, 0, len);
                            fileOutputStream.flush();
                            progress += len;
                            if (downloadListener != null) {
                                downloadListener.downloadProgress(total, progress);
                            }
                        }
                        inputStream.close();
                        fileOutputStream.close();
                    }
                } catch (Exception e) {
                    if (downloadListener != null) downloadListener.downloadFail();
                    e.printStackTrace();
                } finally {
                    isDownloading = false;
                    if (connection != null) connection.disconnect();
                }
            }
        }).start();

    }

    private static void test(Context mContext){
        final String apkPath = mContext.getExternalCacheDir().getAbsolutePath() + "/hys_his.apk";
        DownloadFileUtil.downloadFile("http://dldir1.qq.com/weixin/android/weixin667android1320.apk", new DownloadFileUtil.DownloadListener() {
            @Override
            public void downloadProgress(long total, long progress) {
                int per = (int) (progress * 100 / total);
                LogUtils.d("下载进度 " + per + "  total = " + total);
                //pre百分比
                if (progress == total) {
                    LogUtils.d("下载完成");
                }
            }

            @Override
            public void downloadFail() {

            }
        }, apkPath);
    }
}
